package com.twohand.utils.Response;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

public class HttpRequest<T>{
    public  T get(Class<T> tClass,String uri) throws JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
        ResponseEntity<String> response =  restTemplate.exchange(uri,HttpMethod.GET,entity,String.class);
        System.out.println(response);
        System.out.println(response.getBody());
        ObjectMapper objectMapper = new ObjectMapper();
        T data = (T) objectMapper.readValue(response.getBody(),tClass);
        return data;
    }
}
